﻿using UnityEngine;
using System.Collections;

public class player_script : BM_LevelObject {

	public float collisionDist = 5;
	public Transform rightArm;
	public Transform leftArm;
	public Transform head;
	public CharacterMotor controller_motor;
	public MouseLook playerLook;
	public MouseLook mouseLook;
	public Transform camera_trans;

	private BM_CollectableToy m_rGrabbedToy = null;
	private RaycastHit hit;
	private GameObject hitObj;

	private AudioSource[]	AudioSteps;
	private float			m_fPlayStepTimeStamp = 0.0f;
	private float			m_fPlayStepEvery = 0.3f;

	private bool inputActive = true;


	public BM_BaseToy.Toy GetLastObject() {
		if(m_rGrabbedToy)
			return m_rGrabbedToy.ToyName;
		else
			return BM_BaseToy.Toy.TOY_NONE;
	}

	public Vector3 GetMovementDirection
	{
		get { return controller_motor.GetDirection(); }
	}

	public override void Awake ()
	{
		base.Awake ();

		AudioSteps = gameObject.GetComponents<AudioSource>();
	}


	// Update is called once per frame
	public override void Update () {

		if(m_rMessageHandler.ReceivedMessageWithID(MessageID.MSGID_START_INTERACTION) != null) {
			inputActive = true;
			controller_motor.canControl = true;
			playerLook.enabled = true;
			mouseLook.enabled = true;
		} else if(m_rMessageHandler.ReceivedMessageWithID(MessageID.MSGID_STOP_INTERACTION) != null) {
			inputActive = false;
			controller_motor.canControl = false;
			playerLook.enabled = false;
			mouseLook.enabled = false;
		}

		if(inputActive) {
			Vector3 pos = camera_trans.position;
			Vector3 dir = camera_trans.forward;

			Debug.DrawRay (pos, dir*collisionDist, Color.red);

			if(Input.GetMouseButtonDown(0) && m_rGrabbedToy != null && !m_rGrabbedToy.QuestActive)
			{
				m_rMessageHandler.SendMessageToOne(m_rGrabbedToy.MessageHandler, new BM_BaseMessage(MessageID.MSGID_TOY_PERFORM_DROP, MessageHandler));
				m_rGrabbedToy = null;
			}
			else if(Physics.Raycast (pos, dir, out hit, collisionDist, 1<<8))
			{
				hitObj = hit.collider.gameObject;
				if(hitObj != null)
				{
					BM_CollectableToy rCollectable = hitObj.transform.parent.gameObject.GetComponent<BM_CollectableToy>();
					BM_DialogueToy rDialogue = hitObj.transform.parent.gameObject.GetComponent<BM_DialogueToy>();
					if(Input.GetMouseButtonDown(0))
					{
						if(rCollectable != null)
						{
							MessageHandler.SendMessageToOne(rCollectable.MessageHandler, new BM_BaseMessage(MessageID.MSGID_TOY_PERFORM_ACTION, MessageHandler));
							m_rGrabbedToy = rCollectable;
						}
						else if(rDialogue != null)
							MessageHandler.SendMessageToOne(rDialogue.MessageHandler, new BM_BaseMessage(MessageID.MSGID_TOY_PERFORM_ACTION, MessageHandler));
					}
					else
					{
						BM_BaseToy rToy = null;
						if(rCollectable != null)
							rToy = rCollectable;
						else if(rDialogue != null)
							rToy = rDialogue;
						
						MessageHandler.SendMessageToAllButMe(new BM_ToySelectionMessage(rToy.ToyName, MessageHandler));
					}

				}
			} else {
				MessageHandler.SendMessageToAllButMe(new BM_ToySelectionMessage(BM_BaseToy.Toy.TOY_NONE, MessageHandler));
			}
		}

		// suoni degli step
		if(!Mathf.Approximately(GetMovementDirection.magnitude, 0.0f))
		{
			if(m_fPlayStepTimeStamp < 0.0f)
				m_fPlayStepTimeStamp = Time.time;

			if(Time.time >= m_fPlayStepTimeStamp)
			{
				AudioSteps[Random.Range(0, AudioSteps.Length)].Play();
				m_fPlayStepTimeStamp = Time.time + m_fPlayStepEvery;
			}
		}
		else
			m_fPlayStepTimeStamp = -1.0f;
	}
	
}
